<?php

defined('BASEPATH') or die('forbidden');

class Realtime_Controller Extends Controller {
    private $server_list;
    private $m;
    private $wholeServerHost;

    function _init() {
        //var_dump($_SESSION['WG']);
        check_level(50, segment(0), segment(1), segment(2, 'index'));

        $config = get_config();
        $this->wholeServerHost = $config['host_url'];
        $this->server_list=get_server_list();
    }

    function index() {
        $this->view->display("area/realtime/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $pd['SessionID']="ewewqw";
        $pd['SessionKey']="wqqew";

        $total = 0;
        $data=array();
        foreach ($this->server_list as $k=>$v){
            $pd['SvrID'] = (int)$v['SvrID'];
			$wholeServerHost="https://".$v['SvrOutAddr']."/";
            $re = vpost($wholeServerHost.'gm_online_num',3,json_encode($pd));
            $res=json_decode($re,true);

            $vd["SvrID"] = $v["SvrID"];
            $vd["SvrName"] = $v["SvrName"][1];
            $vd['currentonline']=$res['Num'];
            $vd['registercount']=$res['TotalNum'];
            $vd['todaycount']=$res['TodayLogin'];
            $vd['TodayReg']=$res['TodayReg'];
            $data[$k]=array('id' => $vd['SvrID'], 'cell' => $vd);

            $total = $total+1;
        }

        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $data
        );
        echo json_encode($result);
    }

    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        $allids = $ids = explode(',', $ids);
        if (count($ids) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            $ids = "'" . implode("','", $ids) . "'";
            if ($type == 'accept') {
                $this->db->query("UPDATE {pre}areasample SET state='normal' WHERE id IN ({$ids})");

                insert_log(translate('MENU_AREA_REALTIME'), translate('MULTI_OPEN'), array('ids'=>implode(",", $allids)));
                $c = 1;
            } else if ($type == 'reject') {
                $this->db->query("UPDATE {pre}areasample SET state='close' WHERE id IN ({$ids}) AND state!='hide'");
                insert_log(translate('MENU_AREA_REALTIME'),translate('MULTI_CLOSE'), array('ids'=> implode(",", $allids)));
                $c = 1;
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        json_output($c, $r);
    }

}